<?php
/**
 * Email Controller
 * 
 * @author Falanga F. - Beschi J., falanga.fra@gmail.com - intrip@gmail.com
 * @package whichclub
 * 
 * 
 */
class EmailController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        require_once "models/Db/Event_db.php";
        require_once "models/Utility/Utility.php";

        $utility_handle = new Utility();
        $eventHandle    = new Event_db();

        $pivaList = $eventHandle->getAllPiva();
        foreach ($pivaList as $pivaList2){
                $piva = $pivaList2['piva'];
                $temp = $eventHandle->getUniqueNamebyPiva($piva);
                $allDisco[$piva] = array ($temp['unique_name'], $temp['name']);
        }
        $allDisco = $utility_handle->subval_sort($allDisco,0);
        $this->view->allDisco = $allDisco;

        foreach ($pivaList as $pivaList2){
            $piva = $pivaList2['piva'];
            $temp = $eventHandle->getPaeseDsicoByPiva($piva);
            $paese[$piva] = $temp['paese'];
            //$allDisco[] = $temp['name'];
            //$i++;
        }
        $this->view->paese = array_unique($paese);


        $categoryList = $eventHandle->getAllCategory();
        $this->view->category = $categoryList;

        $zonaList = array('Brescia','Verona','Mantova','Cremona');
        $this->view->zonaList = $zonaList;
    }

    public function indexAction()
    {
        // action body
    }

    public function sendMailAction()
    {
        // action body
        $MailObj = new Zend_Mail();

        $emailMessage="Clicca qui per confermare la tua prenotazione.";
        $fromEmail="info@whichclub.it";
        $fromFullName="WhichClub.it - Booking Events";
        $to="whichclub@gmail.com";
        $subject="Conferma iscrizione";

        $MailObj->setBodyText($emailMessage);
        $MailObj->setFrom($fromEmail,$fromFullName);
        $MailObj->addTo($to);
        $MailObj->setSubject($subject);

        try{
            $MailObj->send();
            echo "E-mail sent successfully";
        } catch (Zend_Mail_Exception $e){
            echo "No E-mail sent.";
            //error messagge
        }
        $this->_helper->viewRenderer->setNoRender();
    }

    public function smtpSendMailAction()
    {
        // action body

        /*
         * ricevo i valori della prenotazione dell'utente dall'azione
         * subscribe-disco-event
         */
        $id_event		=$this->getRequest()->getParam('id_event');
        $nome			=$this->getRequest()->getParam('nome');
        $tavoli			=$this->getRequest()->getParam('numero_persone');
        $email			=$this->getRequest()->getParam('email');
        $phone			=$this->getRequest()->getParam('phone');
        $numero_tavoli	=$this->getRequest()->getParam('numero_tavoli');
        
        //echo "<br />$id_event<br />$nome<br />$tavoli<br />$email<br />$phone<br />$numero_tavoli<br />";

        // configuro l'host da cui mando l'email
        $configInfo = array('auth'=>'login', 'username'=>'info@whichclub.it','password'=>'********');
        $smtpHost = new Zend_Mail_Transport_Smtp('smtp.whichclub.it',$configInfo);
        
        // preparo il corpo della mail
        $emailMessage="<h4>Grazie per aver scelto WihchClub.it.</h4>
                       <p>Ora per confermare la tua prenotazione devi semplicemente cliccare sul link seguente:<br />
                       http://www.whichclub.it/subscribe/validate-success-disco?email=".$email."&id_event=".$id_event."&numero_tavoli=".$numero_tavoli."</p><br /><br /><br />
                       Nel caso il link non funzionasse contattaci a: whichlcub@gmail.com<br />
                       All rigth reserved @whichclub.it - Booking Events";
        $fromEmail="whichclub@gmail.com";
        $fromFullName="WhichClub.it - Booking Events";
        //$to="whichclub@gmail.com";
        $to=$email;
        $bcc="info@whichclub.it";
        $subject="Conferma iscrizione";

        $MailObj = new Zend_Mail();
        $MailObj->setBodyHtml($emailMessage);
        $MailObj->setFrom($fromEmail,$fromFullName);
        $MailObj->addTo($to);
        $MailObj->addBcc($bcc);
        $MailObj->setSubject($subject);


        try{
            $MailObj->send($smtpHost);
            //$MailObj->send();
            $message=true;
            $this->view->message=$message;            

        } catch (Zend_Mail_Exception $e){
            echo $e->getMessage();
            //error messagge
        }
 
    }

}
